package com.yksj.healthtalk.db;

import com.yksj.healthtalk.net.socket.SmartFoxClient;

import android.content.Context;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;

/**
 * 客户对应数据库,实现对数据库创建,更新
 * 
 * @author zhao yuan
 * @version 3.0
 */
public class ChatUserOpenHelper extends SQLiteOpenHelper {
	
	private static final int DATABASE_VERSION = 13;// 数据库版本
	private static String database_name = "talk_db";// 数据库名称
	
	public ChatUserOpenHelper(Context context, String name) {
		super(context, name + database_name, null, DATABASE_VERSION);
		if (name == null || "".equals(name))
			throw new IllegalStateException("数据库名称错误" + name + database_name+"当前用户的名字__"+SmartFoxClient.getLoginUserInfo().getUsername());
	}

	@Override
	public void onCreate(SQLiteDatabase db) {
		createTable(db);
	}

	@Override
	public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
		try {
			db.execSQL("alter table friend_list add column blacklist integer default 0");
			db.execSQL("alter table group_list add column recordDesc String ");
			db.execSQL("alter table group_list add column groupLevel String ");
			db.execSQL("alter table group_list add column groupflag Boolean ");
			db.execSQL("alter table group_list add column charge Boolean ");
			db.execSQL("alter table group_list add column isReleaseSystemMessage Boolean ");
			db.execSQL("alter table group_list add column inceptMessage Boolean ");
			db.execSQL("alter table friend_list add column relationType INTEGER");
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	
	/**
	 * 创建表
	 * @param db
	 */
	private void createTable(SQLiteDatabase db) {
		// 朋友聊天历史
		final StringBuffer sqlBuffer = new StringBuffer(
				Tables.TableDB.CREATE_TABLE_IF_NOT_EXISTS);
		sqlBuffer.append(Tables.TableChatMessage.TABLE_CHAT_MESSAGE);
		sqlBuffer.append(" (");
		sqlBuffer.append(Tables.TableChatMessage.MESSAGE_ID
				+ " INTEGER PRIMARY KEY AUTOINCREMENT,"
				+ Tables.TableChatMessage.MSG_SERVER_ID + " TEXT UNIQUE ON CONFLICT IGNORE,"
				+ Tables.TableChatMessage.MSG_SERVER_DELET_STATE + " TEXT,"
				+ Tables.TableChatMessage.MESSAGE_TYPE + " INT,"
				+ Tables.TableChatMessage.RECEIVER_ID + " INTEGER,"
				+ Tables.TableChatMessage.SENDER_ID + " INTEGER,"
				+ Tables.TableChatMessage.READ_TAG + " INT,"
				+ Tables.TableChatMessage.ISSEND + " INT,"
				+ Tables.TableChatMessage.MESSAGE_CONTENT + " TEXT,"
				+ Tables.TableChatMessage.MESSAGE_JSONCOTENT + " TEXT,"
				+ Tables.TableChatMessage.MESSAGE_PATH + " TEXT,"
				+ Tables.TableChatMessage.SENDSTATE + " INT,"
				+ Tables.TableChatMessage.DOWNORUPSTATE + " INT,"
				+ Tables.TableChatMessage.VOICE_LENGTH + " FLOAT,"
				+ Tables.TableChatMessage.SIZE + " INT,"
				+ Tables.ChatTable.TIME + " TEXT" + ")");
		db.execSQL(sqlBuffer.toString());
		// 收藏
		sqlBuffer.setLength(0);
		sqlBuffer.append(Tables.TableDB.CREATE_TABLE_IF_NOT_EXISTS
				+ Tables.TableNewsCollection.TABLE_NEWS_COllECTION + " ("
				+ Tables.TableNewsCollection.NEWS_CLLECTION_ID
				+ " INTEGER PRIMARY KEY AUTOINCREMENT,"
				+ Tables.TableNewsCollection.NEWS_ID + " TEXT,"
				+ Tables.TableNewsCollection.NEWS_TYPE + " INT,"
				+ Tables.TableNewsCollection.NEWS_TITLE + " TEXT,"
				+ Tables.TableNewsCollection.NEWS_CONTENT + " TEXT,"
				+ Tables.TableNewsCollection.NEWS_TIME + " INTEGER,"
				+ Tables.TableNewsCollection.TYPE_TITLE + " TEXT,"
				+ Tables.TableNewsCollection.NEWS_CLLECTION_TIME + " INTEGER"
				+ ")");
		db.execSQL(sqlBuffer.toString());
		
		
		// 虚拟医生聊天历史
		sqlBuffer.setLength(0);
		sqlBuffer.append("CREATE TABLE IF NOT EXISTS ");
		sqlBuffer.append(Tables.TableDoctor.TABLE_VIRTUAL_DOCTOR_MESSAGE);
		sqlBuffer.append("(");
		sqlBuffer.append("message_id INTEGER PRIMARY KEY AUTOINCREMENT,");
		sqlBuffer.append("message_type INT,");
		sqlBuffer.append("sendstate INT,");
		sqlBuffer.append("message_content TEXT,");
		sqlBuffer.append("receiver_id INTEGER,");
		sqlBuffer.append("sender_id INTEGER,");
		sqlBuffer.append("read_tag INT,");
		sqlBuffer.append("msg_content TEXT,");
		sqlBuffer.append("isSend INT,");
		sqlBuffer.append("messageCode TEXT,");
		sqlBuffer.append("messageClass TEXT,");
		sqlBuffer.append("retFeatur int,");
		sqlBuffer.append("functionId TEXT,");
		sqlBuffer.append("linkDialog TEXT,");
		sqlBuffer.append("ltalkName TEXT,");
		sqlBuffer.append("serviceLinkId TEXT,");
		sqlBuffer.append("serviceLinkName TEXT,");
		sqlBuffer.append("largeinfoId TEXT,");
		sqlBuffer.append("talkname TEXT,");
		sqlBuffer.append("time TEXT");
		sqlBuffer.append(")");
		db.execSQL(sqlBuffer.toString());

		// 朋友列表
		sqlBuffer.setLength(0);
		sqlBuffer.append("CREATE TABLE IF NOT EXISTS ["
				+ Tables.TableFriend.TABLE_FRIEND + "] (" + "[id] CHAR, "
				+ "[customerId] INTEGER, " + "[recent] INTEGER, "
				+ "[iscollect] BOOL, " + "[attention_to] INTEGER, "
				+ "[attention_for] INTEGER, " + "[blacklist] INTEGER, "
				+ "[name] TEXT, " + "[realname] TEXT, " + "[account] TEXT, "
				+ "[description] TEXT, " + "[cusmesssage] TEXT, "
				+ "[locus] TEXT, " + "[sex] TEXT, " + "[age] TEXT, "
				+ "[doctorTitle] TEXT, " + "[doctorunitAdd] TEXT, "
				+ "[doctorunit] TEXT, " + "[dwellingplace] TEXT, "
				+ "[face_head_norml] CHAR, " + "[face_head_big] CHAR, "
				+ "[groupName] TEXT, " + "[not_readsmg] INT,"
				+ "time DATETIME," + "[relationType] INTEGER,"
				+ "PRIMARY KEY(id,customerId))");
		db.execSQL(sqlBuffer.toString());

		// 组列表
		sqlBuffer.setLength(0);
		sqlBuffer.append("CREATE TABLE IF NOT EXISTS ["
				+ Tables.TableFriend.TABLE_GROUP + "] (" + "[id] CHAR, "
				+ "[customerId] CHAR, " + "[recent] INTEGER, "
				+ "[iscollect] BOOL, " + "[name] TEXT, " + "[note] TEXT, "
				+ "[personNumber] INTEGER," + "[createTime] TEXT, "
				+ "[infoLayid] TEXT, " + "[groupClass] INTEGER, "
				+ "[createCustomerID] TEXT, " + "[limitNnum] TEXT, "
				+ "[face_head_norml] CHAR, " + "[face_head_big] CHAR, "
				+ "[infoLayName] CHAR, " + "time DATETIME,"
				+ "[recordDesc] TEXT," + "[groupLevel] CHAR,"
				+ "[groupflag] BOOL," + "[charge] BOOL,"
				+ "[isReleaseSystemMessage] BOOL," + "[inceptMessage] BOOL,"
				+ "[not_readsmg] INT," + "PRIMARY KEY(id,customerId))");
		db.execSQL(sqlBuffer.toString());
		sqlBuffer.setLength(0);
		//
		sqlBuffer
				.append("CREATE TABLE IF NOT EXISTS ["
						+ Tables.TableFriend.ALL_FRIEND_GROUP
						+ "] ([id] INTEGER , [customerId] CHAR, [name] CHAR PRIMARY KEY)");
		db.execSQL(sqlBuffer.toString());
		sqlBuffer.setLength(0);
	}

}
